package com.aiju.cash.ui.multiple_shops.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.aiju.bean.Good;
import com.aiju.cash.R;
import com.aiju.common.image.AiJuImageLoaderProxy;
import com.aiju.utils.Util;

import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-06-15 19:33
 * @version: V1.0
 * @Description: 关联商品分类适配器
 */
public class GoodAdapter extends RecyclerView.Adapter<GoodAdapter.ViewHolder> {

    private List<Good> goods;

    private boolean delete;

    public GoodAdapter(List<Good> goods) {
        this.goods = goods;
    }

    @Override
    public GoodAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_relation_team, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        final Good good = goods.get(position);

        viewHolder.nameTextView.setText(good.getName());
        if (good.isWeighGood()) {
            viewHolder.priceTextView.setText(good.getPrice() + "/" + good.getUnit());
        } else {
            viewHolder.priceTextView.setText(good.getPrice());
        }
        viewHolder.rootView.setSelected(good.getCurrentRelation().equals("1") ? true : false);
        if (good.isRelation()) {
            viewHolder.relationTextView.setTextColor(Util.getColor(R.color.app_display_color));
            viewHolder.relationTextView.setText("已关联");
        } else {
            viewHolder.relationTextView.setTextColor(Util.getColor(R.color.color_999999));
            viewHolder.relationTextView.setText("未关联");
        }
        if (delete) {
            viewHolder.relationTextView.setVisibility(View.GONE);
            viewHolder.imageViewDelete.setVisibility(View.VISIBLE);
            viewHolder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    v.setSelected(!v.isSelected());
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(position);
                    }
                }
            });
        } else {
            viewHolder.relationTextView.setVisibility(View.VISIBLE);
            viewHolder.imageViewDelete.setVisibility(View.GONE);
            viewHolder.rootView.setOnClickListener(null);
        }
        AiJuImageLoaderProxy.getInstance().displayImage(good.getImageUrl(), viewHolder.imageViewTeam, R.mipmap.icon_empty);
    }

    @Override
    public int getItemCount() {
        return goods.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        View rootView;
        TextView nameTextView;
        TextView priceTextView;
        TextView relationTextView;
        ImageView imageViewTeam;
        ImageView imageViewDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView;
            nameTextView = (TextView) itemView.findViewById(R.id.tv_good_name);
            priceTextView = (TextView) itemView.findViewById(R.id.tv_good_price);
            relationTextView = (TextView) itemView.findViewById(R.id.tv_good_relation);
            imageViewTeam = (ImageView) itemView.findViewById(R.id.iv_good_pic);
            imageViewDelete = (ImageView) itemView.findViewById(R.id.iv_good_delete);
        }
    }

    //点击事件
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }

    public void setDelete(boolean delete) {
        this.delete = delete;
        notifyDataSetChanged();
    }

}
